/**
 * @Description: WoShop
 * @Author: Parker
 * @Copyright: 武汉一一零七科技有限公司©版权所有
 * @Link: www.wo-shop.net
 * @Contact: QQ:2487937004
 */
const Fly = require('flyio/dist/npm/wx')
const fly = new Fly

import {SERVERTAPI} from '../config/index'
import {showToast} from '../utils/index'
fly.config.timeout = 20000
fly.config.baseURL = SERVERTAPI;

fly.interceptors.request.use(request => {
	request.body.token = uni.getStorageSync('token')
	request.body.device_token = uni.getStorageSync('devicetoken')
	return request
})

fly.interceptors.response.use(
	(response) => {
		uni.hideLoading()
		uni.stopPullDownRefresh()
		if (response.data && response.data.status == 200) {
			return response.data
		}

		if (response.data && response.data.status === 402) {
			uni.setStorageSync('token', '');

			uni.navigateTo({
				url: '/pagesB/login/login'
			})
			return response.data
		}

		if (response.data && response.data.status == 300) {
			// utils.showToast(`status:${response.data.status} -- ${response.data.mess}`)
			return response.data
		}
		if (response.data && response.data.status != 200) {
			if(response.data.data.tip_show && response.data.data.tip_show == 'close'){
			    // 后端传递过来tip_show，控制前端在非200状态下是否有弹窗提示，close不显示弹窗提示
			}else{
				showToast(`${response.data.mess}`)
			}
			
			return response.data
		}
		if (response.data && response.data.status == 500) {
			return response.data
		}
	},
	(err) => {
		uni.hideLoading()
		uni.stopPullDownRefresh()
		if (err.status == 0) {
			//NetworkError网络错误
			showToast('网络错误')
			return err
		}

		if (err.status == 500) {
			//ServerError服务器错误
			showToast('服务器错误')
			return err
		}

	}
)

export default fly